<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link rel="stylesheet" href="../layui/css/layui.css" />
        <style>
            html,body{
                background-color: #ccc;
                height:100%;
                display: flex;
                justify-content: center;
                align-items: center;
            }
            .card{
                width: 500px;
            }
        </style>
    </head>
    <body>
        <div class="layui-card card">
            <div class="layui-card-header">注册</div>
            <div class="layui-card-body">
                <form class="layui-form" lay-filter="regForm" action="#">
                    <div class="layui-form-item">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-block">
                            <input
                                type="text"
                                name="username"
                                lay-verify="required|username"
                                placeholder="请输入标题"
                                autocomplete="off"
                                class="layui-input"
                                lay-verType="tips"
                            />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">密码</label>
                        <div class="layui-input-block">
                            <input
                                type="password"
                                name="pwd"
                                lay-verify="required|pwd"
                                placeholder="请输入密码"
                                autocomplete="off"
                                class="layui-input"
                            />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">确认密码</label>
                        <div class="layui-input-block">
                            <input
                                type="password"
                                name="password"
                                lay-verify="required|confirm"
                                placeholder="请输入密码"
                                autocomplete="off"
                                class="layui-input"
                            />
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button
                                class="layui-btn"
                                lay-submit
                                lay-filter="formDemo"
                            >
                                立即提交
                            </button>
                            <button
                                type="reset"
                                class="layui-btn layui-btn-primary"
                            >
                                重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <script src="../layui/layui.js"></script>
        <script src="../js/mock-min.js"></script>
        <script src="../lesson13/data.js"></script>
        <script src="../lesson13/ajax.js"></script>
        <script>
            layui.define(function () {
                let {$,layer,form,table} = layui;

                //…
                // layer.msg('Hello World');
                form.verify({
                    username:async function(value,item){
                        if(!/^\w{6,20}$/.test(value)){
                            return "用户名格式不正确"
                        }
                        let res = await ajax({
                            type:"post",
                            url:"/usernameExist",
                            data:{
                                username:value
                            }
                        });
                        if(res.status == 0){
                            return "用户名不可用"
                        }
                    },
                    pwd:[/^.{6,}$/,"密码格式不正确"],
                    confirm:function(value,item){
                        console.log($("[name=pwd]").val());
                        console.log(form.val("regForm").pwd);
                        if($("[name=pwd]").val() != value){
                            return true;
                        }
                    },
                });
                 form.on("submit(formDemo)",function(){
                    console.log(form.val("regForm"));
                    // let res = await new Promise(function(resolve){
                    //     $.ajax({
                    //         type:"post",
                    //         url:"/reg",
                    //         processData:false,
                    //         data:form.val("regForm"),
                    //         success(res){
                    //            resolve(res);
                    //         }
                    //     });
                      
                    // });
                    (async function(){
                        let res = await ajax({
                           type:"post",
                           url:"/reg",
                           data:form.val("regForm")
                       });
                        console.log("Res",res);
                    }())
                    
                    // layer.alert('注册成功');
                    // alert('1')
                    return false;
                });
            });
        </script>
    </body>
</html>
